package com.zj.study.utils.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.springframework.util.StringUtils;


/**
 * @author 赵赳
 * @since 2022/9/27 14:27
 */
public class FileZip {

  /**
   * @param inputFileName 你要压缩的文件夹(整个完整路径)
   * @param zipFileName   压缩后的文件(整个完整路径)
   * @throws Exception
   */
  public static Boolean zip(String inputFileName, String zipFileName) throws Exception {
    zip(zipFileName, new File(inputFileName));
    return true;
  }

  private static void zip(String zipFileName, File inputFile) throws Exception {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    zip(out, inputFile, "");
    out.flush();
    out.close();
  }

  private static void zip(ZipOutputStream out, File f, String base) throws Exception {
    if (f.isDirectory()) {
      File[] fl = f.listFiles();
      out.putNextEntry(new ZipEntry(base + "/"));
      base = base.length() == 0 ? "" : base + "/";
      assert fl != null;
      for (File file : fl) {
        zip(out, file, base + file.getName());
      }
    } else {
      out.putNextEntry(new ZipEntry(base));
      FileInputStream in = new FileInputStream(f);
      int b;
      while ((b = in.read()) != -1) {
        out.write(b);
      }
      in.close();
    }
  }

  public static void main(String[] temp) {
    try {
      //String span = new String(new byte[]{0x03});
      //String str =
      //    "J00040100000000" + span + "J00040100000000" + span + "20220927" + span + "183338" + span
      //        + "1001" + span + "C1574708838895824898" + span + "2022092750095782" + span + "C1574708838895824898" + span
      //        + "1" + span + "1" + span + "1" + span + "1" + span + "10.00" + span + "10.00" + span + "0"
      //        + span + "0" + span + "0" + span + "0" + span + "0" + span + "支付10元"+"\n"+
      //        "J00040100000000" + span + "J00040100000000" + span + "20220927" + span + "184300" + span
      //        + "1001" + span + "PR1574711164809359361" + span + "2022092750095782" + span + "PR1574711164809359361" + span
      //        + "2" + span + "2" + span + "1" + span + "1" + span + "10.00" + span + "9.60" + span + "0"
      //        + span + "0" + span + "0" + span + "0" + span + "0" + span + "退款10元";
      //System.out.println(str);
      //File file = new File("src/main/resources/templates/100005410");
      //var newFile = file.createNewFile();
      //System.out.println("newFile = " + newFile);// write
      //FileWriter fw = new FileWriter(file, true);
      //BufferedWriter bw = new BufferedWriter(fw);
      //bw.write(str);
      //bw.flush();
      //bw.close();
      //fw.close();
      //zip("src/main/resources/static/file/J0004010000000010076012022092901",
      //    "src/main/resources/static/file/J0004010000000010076012022092901.zip");//你要压缩的文件夹和压缩后的文件


    } catch (Exception ex) {
      ex.printStackTrace();
    }


  }


}
